#include "misc.h"
#include "matrix.h"
#include "util.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define FUNC_EVAL(token) printf(#token " = %d\n", token)

static matrix m1, m2, m3, m4, res1, res2;


int main(int argc, char *argv[]) {

    int row=3, col=5;

    if (argc == 3) { 
        row = atoi(argv[1]);
        col = atoi(argv[2]);
    }


    /* create a matrix m1 */
    FUNC_EVAL(row);
    FUNC_EVAL(col);
    FUNC_EVAL(init_mat(&m1,row,col));
    FUNC_EVAL(fill_rand(&m1)); 

    /* test if print_mat works */
    FILE *fp = fopen("testdata/m1.txt", "w");
    FUNC_EVAL(print_mat(fp,&m1));
    fclose(fp);

    /* test if read_mat works */
    fp = fopen("testdata/m1.txt", "r");
    FUNC_EVAL(read_mat(fp, &m2));
    fclose(fp);

    FUNC_EVAL(mat_equal(&m1,&m2));

    /* test if dump_mat works */
    fp = fopen("testdata/m2.mat", "w");
    FUNC_EVAL(dump_mat(fp, &m2));
    fclose(fp);

    /* test if load_mat works */
    fp = fopen("testdata/m2.mat", "r");
    FUNC_EVAL(load_mat(fp, &m3));
    fclose(fp);

    FUNC_EVAL(mat_equal(&m2,&m3));
    FUNC_EVAL(init_mat(&m4,row,col));
    FUNC_EVAL(fill_zero(&m4));
    FUNC_EVAL(add_mat(&m3,&m4,&res1));
    FUNC_EVAL(mat_equal(&m3,&res1));
    FUNC_EVAL(fill_ident(&m4));
    FUNC_EVAL(mult_mat(&m3,&m4,&res2));
    FUNC_EVAL(mat_equal(&m3,&res2));

    matrix A, At;
    fp = fopen("testdata/A.bin", "r");
    FUNC_EVAL(load_mat(fp, &A));
    fclose(fp);
    
    print_mat(stdout, &A);
    FUNC_EVAL(trans_mat(&A, &At));
    print_mat(stdout, &At);

    return 0;
}
